package com.example.administrator.fishbone.Fragment;


import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;

import com.example.administrator.fishbone.Fragment.HoneFragment.BacklogFragment;
import com.example.administrator.fishbone.Fragment.HoneFragment.DynamicFragment;
import com.example.administrator.fishbone.Fragment.HoneFragment.InboxFragment;
import com.example.administrator.fishbone.R;
import com.example.administrator.fishbone.adapter.MyFragmentPagerAdapter;
import com.example.administrator.fishbone.bean.BackHandledFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * A simple {@link Fragment} subclass.
 */
public class homeFragment extends BackHandledFragment implements ViewPager.OnPageChangeListener {

    private RadioButton radiodynamic;
    private RadioButton radiobacklog;
    private RadioButton radioinbox;
    private RadioGroup radioGroup;
    private MyFragmentPagerAdapter adapter;
    private ViewPager viewPager;
    BacklogFragment backlogFragment;
    DynamicFragment dynamicFragment;
    InboxFragment inboxFragment;
    private List<Fragment> list = new ArrayList<Fragment>();


    private ImageButton mImage;
    PopupWindow popupWindow;
    GridView gridView;

    LayoutInflater inflater;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        return inflater.inflate(R.layout.fragment_home, container, false);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mImage = (ImageButton) getView().findViewById(R.id.home_image);
        mImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopuWindow(v);
            }
        });

        adapter = new MyFragmentPagerAdapter(getChildFragmentManager(), list);
        backlogFragment = new BacklogFragment();
        inboxFragment = new InboxFragment();
        dynamicFragment = new DynamicFragment();

        list.add(dynamicFragment);
        list.add(backlogFragment);
        list.add(inboxFragment);

        viewPager = (ViewPager) getView().findViewById(R.id.ViewPager);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(1);
        viewPager.setOnPageChangeListener(this);

        radiodynamic = (RadioButton) getView().findViewById(R.id.home_dynamic);
        radiobacklog = (RadioButton) getView().findViewById(R.id.home_backlog);
        radioinbox = (RadioButton) getView().findViewById(R.id.home_inbox);

        radioGroup = (RadioGroup) getView().findViewById(R.id.home_radio_group);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.home_dynamic:
                        viewPager.setCurrentItem(0, true);
                        break;
                    case R.id.home_backlog:
                        viewPager.setCurrentItem(1, true);
                        break;
                    case R.id.home_inbox:
                        viewPager.setCurrentItem(2, true);
                        break;
                }
            }
        });
        radiodynamic.setChecked(true);
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                radiodynamic.setChecked(true);
                break;
            case 1:
                radiobacklog.setChecked(true);
                break;
            case 2:
                radioinbox.setChecked(true);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    private void showPopuWindow(View v) {
//        inflater = (LayoutInflater) getActivity().getSystemService(Context.LAUNCHER_APPS_SERVICE);
//        View view = inflater.inflate(R.layout.activity_popu_gridview,null,false);

        View view = LayoutInflater.from(getActivity()).inflate(R.layout.activity_popu_gridview, null, false);
        gridView = (GridView) view.findViewById(R.id.popu_gridView);
       view.setOnTouchListener(new View.OnTouchListener() {
           @Override
           public boolean onTouch(View v, MotionEvent event) {
               if (popupWindow != null) {
                   popupWindow.dismiss();
               }
               return false;
           }
       });
        List<Map<String, Object>> maplist = new ArrayList<Map<String, Object>>();
        int[] icon = new int[]{R.drawable.create_new_chat,
                R.drawable.create_new_task,
                R.drawable.create_new_project,
                R.drawable.create_new_email,
                R.drawable.create_new_postbar,
                R.drawable.create_new_report};
         String[] text = new String[]{
                "聊天",
                "任务",
                "项目",
                "邮件",
                "同事圈",
                "工作总结"};
        for (int i = 0; i < icon.length; i++) {
        Map<String, Object> map = new HashMap<String, Object>();
            map.put("icon", icon[i]);
            map.put("text", text[i]);
            maplist.add(map);
        }
        String[] from = {"icon", "text"};
        int[] to = {R.id.popu_gridView_image, R.id.popu_gridview_text};
        SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(), maplist, R.layout.popu_gridview_item,
             from,to);
        if (gridView != null) {
        gridView.setAdapter(simpleAdapter);
        }
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT,true);
        popupWindow.setFocusable(true);
        popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
//        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.masking_bg));
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
        WindowManager manager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
//        int xPos = popupWindow.getWidth() + popupWindow.getWidth();
     gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
         @Override
         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
             FragmentManager fm = getActivity().getSupportFragmentManager();
             FragmentTransaction ft = fm.beginTransaction();
             ft.add(android.R.id.content,new HomeAddFragment(),"HomeAddFragment");
             ft.addToBackStack("HomeAddFragment");
             ft.commit();
             popupWindow.dismiss();
         }
     });

      popupWindow.showAtLocation(getView(), Gravity.NO_GRAVITY,0,0);
    }


    @Override
    public boolean onBackPressed() {
        return false;
    }
}
